import { queryDictData, queryPerson } from '@/utils/common';
import AdSelect from '@/components/AdSelect';
import AdSearch from '@/components/AdSearch';

export const codes = {
  select: 'BILLPAGE_SELECT',
  remove: 'BILLPAGE_DEL',
  add: 'BILLPAGE_INSERTSAVECOMMIT',
  add2: 'BILLPAGE_INSERTSAVEDRAFT', //保存提交
  showDetail: 'BILLPAGE_VIEW',
  removeInfo: 'BILLPAGE_REMDETAIL',
  addInfo: 'BILLPAGE_ADDDETAIL',
  page: 'BILLPAGE',
  edit: 'BILLPAGE_SAVE',
  edit2: 'BILLPAGE_SAVECOMMIT', //编辑时的保存提交
  commitBatch: 'BILLPAGE_COMMITBATCH',
  delete: 'ELE_DRIVERPAPERS_DELETE',
};

export const billStatusList = [
  { code: 'DRAFT', value: '草稿' },
  { code: 'AUDITING', value: '审核中' },
  { code: 'BACK', value: '退回' },
  { code: 'FINISH', value: '完成' },
];
//保养类型支付方式：
export const upkeepPayMethod = [{ code: 'CASH', value: '现金' }, { code: 'MONTH', value: '月结' }];

export const allUrl = {
  companyList: 'tms/tms-bill/selectSpplierList_s',
  userList: 'tms/tms-bill/selectUserList_s',
  supplierList: 'tms/tms-bill/selectSpplierList_s',
  supplierDetail: 'mds/d-customer/viewCompany',
  userDetail: 'mds/mds-user/viewDetails',
  bankList: 'tms/tms-bill/selectBankCard',
};

export const routeUrl = {
  add: '/carAccount/billList/addBill',
  edit: '/carAccount/billList/editBill',
  print: '/print/bill',
};

export const allDispatchType = {
  list: 'vehicle/selectBillList',
  detail: 'vehicle/selectBillDetail',
  remove: 'vehicle/removeBill',
  info: 'vehicle/selectBillInfo',
  borrowInfo: 'vehicle/selectBorrowInfo',
  add: 'vehicle/billOperate',
  removeInfo: 'vehicle/removeBillInfo',
  addInfoList: 'vehicle/seleceBillAddInfo',
  addInfoDetail: 'vehicle/addBillInfoDetail',
  ownCompany: 'component/queryOwnCompany',
  value: 'vehicle/allValus',
  commit: 'vehicle/commitBill',
  detailsInfo: 'vehicle/detailsInfo',
  detailInfoDownload: 'vehicle/detailInfoDownload',
  queryBillOAPrint: 'vehicle/queryBillOAPrint',
  recallTmsBill: 'vehicle/recallTmsBill',
};

// UNSEALED 草稿、 SUBMITTED 已提交、 CONFIRM 已确认
export const incidentalStatusList = [
  { code: 'UNSEALED', value: '草稿' },
  { code: 'CONFIRM', value: '已确认' },
  { code: 'SUBMITTED', value: '已提交' },
];

export function renderTableAdSelect({ key, data, value, props }) {
  let params = { onlyRead: true, value };
  if (key) {
    const { dictObject } = props;
    params = { data: dictObject[key], payload: { code: [key] }, ...params };
  } else {
    params = { data, ...params };
  }
  return <AdSelect {...params} />;
}

export function renderTableAdSearch({ value, props }) {
  if (!value || !searchValue) return '';
  const { searchValue } = props;
  const params = {
    onlyRead: true,
    value: searchValue[value],
    label: 'loginName',
    name: 'sysName',
  };
  return <AdSearch {...params} />;
}

export function selectBillList({ payload = {}, props } = {}) {
  const { dispatch, searchValue } = props;
  dispatch({
    type: allDispatchType.list,
    payload,
    callback: data => {
      if (!data) return;
      queryPerson({ data, props: props, url: allUrl.userList });
    },
  });
}

export function selectBillDetailAndInfo({ type, payload, props, callback }) {
  const { dispatch } = props;
  dispatch({
    type,
    payload,
    callback: data => {
      if (!data) return;
      callback && callback(data);
    },
  });
}

export function saveAllValues({ payload = {}, props } = {}) {
  const { dispatch } = props;
  dispatch({
    type: allDispatchType.value,
    payload,
  });
}

export function caclTotal(data, text) {
  let total = 0;
  if (data && data.length > 0) {
    data.map((item, i) => {
      if (item && item[text]) {
        total = total + Number(item[text]);
      }
    });
  }
  return total == 0 ? 0 : total.toFixed(2);
}
export function caclAvgPrice(data, text) {
  let total = 0;
  let index = 0;
  if (data && data.length > 0) {
    data.map((item, i) => {
      if (item && item[text]) {
        total = total + Number(item[text]);
        index++;
      }
    });
  }
  const avg = total / index;
  return avg == 0 ? 0 : avg.toFixed(2);
}

//车辆统计 (司机证件 类型无车辆统计)
export function getBillVehicleIDetails(dispatch, detailId, type, open) {
  if (!type || type === 'DRIVERPAPER' || type === 'MISCELLANEOUS') return;
  dispatch({
    type: 'vehicle/seleceBillVehicleInfo',
    payload: { id: detailId, type, open },
    callback: data => {
      if (data.length === 0) return;

      // data.push(obj);
    },
  });
}
//加油统计：
export function getBillOilDetails(dispatch, detailId, open) {
  dispatch({
    type: 'vehicle/seleceBillOilInfo',
    payload: { id: detailId, open },
    callback: data => {
      if (data.length === 0) return;
    },
  });
}

export function getBillTaxList(dispatch, detailId) {
  dispatch({
    type: 'vehicle/selectBillTax',
    payload: { id: detailId },
    callback: data => {
      if (data.length === 0) return;
    },
  });
}
